<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Angular,directive," />








  <link rel="shortcut icon" type="image/x-icon" href="/uploads/favicon.ico?v=5.1.1" />






<meta name="description" content="准备实例代码 1var app = angular.module(&apos;app&apos;, []); angular指令定义大致如下 12345app.directive(&apos;directiveName&apos;, function() &amp;#123;  return &amp;#123;   // config  &amp;#125;&amp;#125;) 其中return返回的配置对象包含很多参数，如下一一说明。">
<meta name="keywords" content="Angular,directive">
<meta property="og:type" content="article">
<meta property="og:title" content="Angular directive 各属性详解">
<meta property="og:url" content="https://syjun.github.io/2017/06/29/angularjs-20170629Angular-directive-各属性详解/index.html">
<meta property="og:site_name" content="灰色の幽默">
<meta property="og:description" content="准备实例代码 1var app = angular.module(&apos;app&apos;, []); angular指令定义大致如下 12345app.directive(&apos;directiveName&apos;, function() &amp;#123;  return &amp;#123;   // config  &amp;#125;&amp;#125;) 其中return返回的配置对象包含很多参数，如下一一说明。">
<meta property="og:updated_time" content="2017-07-13T06:56:53.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Angular directive 各属性详解">
<meta name="twitter:description" content="准备实例代码 1var app = angular.module(&apos;app&apos;, []); angular指令定义大致如下 12345app.directive(&apos;directiveName&apos;, function() &amp;#123;  return &amp;#123;   // config  &amp;#125;&amp;#125;) 其中return返回的配置对象包含很多参数，如下一一说明。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://syjun.github.io/2017/06/29/angularjs-20170629Angular-directive-各属性详解/"/>





  <title>Angular directive 各属性详解 | 灰色の幽默</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b8197adc4b3f693d9f3c14c792e39266";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">灰色の幽默</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">路漫漫其修远兮,吾将上下而求索</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://syjun.github.io/2017/06/29/angularjs-20170629Angular-directive-各属性详解/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="石艷軍">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色の幽默">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Angular directive 各属性详解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-29T15:20:56+08:00">
                2017-06-29
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Angular/" itemprop="url" rel="index">
                    <span itemprop="name">Angular</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i> 浏览
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>准备实例代码</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> app = angular.module(<span class="string">'app'</span>, []);</div></pre></td></tr></table></figure>
<p>angular指令定义大致如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">app.directive(<span class="string">'directiveName'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> &#123;</div><div class="line">   <span class="comment">// config</span></div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>其中return返回的配置对象包含很多参数，如下一一说明。</p>
<a id="more"></a>
<h2 id="restrict"><a href="#restrict" class="headerlink" title="restrict"></a><code>restrict</code></h2><p>值为字符串，可选参数，指明指令在DOM中以什么形式被声明</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- E (element) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">directiveName</span>&gt;</span><span class="tag">&lt;/<span class="name">directiveName</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- A (attribute) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">directiveName</span>=<span class="string">"expression"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- C (class) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"directiveName"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- M(comment) --&gt;</span></div><div class="line"><span class="comment">&lt;!--directive:directiveName expression--&gt;</span></div></pre></td></tr></table></figure>
<p>默认值为 <code>restrict: &#39;EA&#39;</code> ，表示可以在DOM里面可以用元素形式和属性形式被声明。一般来说，如果你想创建一个自己模板的组件时，则使用元素形式，但是仅仅是为已有元素添加功能的话，就使用属性名。</p>
<p>如果想要支持IE8，则最好使用属性和类形式来定义，另外从angular 1.3.X开始，已经放弃支持IE8了。</p>
<h2 id="priority"><a href="#priority" class="headerlink" title="priority"></a>priority</h2><p>数字，可选参数，致命指令的优先级，若在单个DOM元素上有多个指令，则优先级高的先执行。</p>
<p>当然，设置指令的优先级不太常用，但是比较特殊的例子是，内置指令ng-repeat的优先级为1000，而ng-init的优先级为 450。</p>
<h2 id="terminal"><a href="#terminal" class="headerlink" title="terminal"></a>terminal</h2><p>布尔型，可选参数，可以被设置为true或者false，若设置为true，则优先级低于此指令的其他指令则无效，不会被调用优先级相同任然会执行。</p>
<h2 id="template"><a href="#template" class="headerlink" title="template"></a>template</h2><p>字符串或者函数，可选参数。</p>
<p>可以是一段html文本</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">app.directive(<span class="string">'hello'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> &#123;</div><div class="line">    <span class="attr">template</span>: <span class="string">'&lt;div&gt;&lt;h3&gt;Hello, world!&lt;/h3&gt;&lt;/div&gt;'</span></div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>使用如下</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">hello</span>&gt;</span><span class="tag">&lt;/<span class="name">hello</span>&gt;</span></div></pre></td></tr></table></figure>
<p>渲染结果为</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">hello</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">h3</span>&gt;</span>Hello, world!<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">hello</span>&gt;</span></div></pre></td></tr></table></figure>
<p>也可以是一个函数，可接受两个参数Element与Attrs</p>
<p>其中Element是指使用此指令的元素，而Attrs则是实例的属性，它是由一个元素上所有属性组成的集合，形如</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">  title: &apos;test&apos;,</div><div class="line">  id: &apos;testDiv&apos;,</div><div class="line">  &quot;class&quot;: &apos;demo&apos;,</div><div class="line">  input: &apos;text&apos;,</div><div class="line">  ...</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>下面让我们看看template是一个函数时的情况</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">app.directive(<span class="string">'hello'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> &#123;</div><div class="line">    <span class="attr">template</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, attrs</span>) </span>&#123;</div><div class="line">      <span class="keyword">return</span> <span class="string">'&lt;div&gt;'</span>+ attrs.title +<span class="string">'&lt;/div&gt;'</span></div><div class="line">    &#125;</div><div class="line">  &#125;;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>html代码</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">hello</span> <span class="attr">title</span>=<span class="string">"message"</span>&gt;</span><span class="tag">&lt;/<span class="name">hello</span>&gt;</span></div></pre></td></tr></table></figure>
<p>渲染结果</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">hello</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>message<span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">hello</span>&gt;</span></div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/LkyEwd" target="_blank" rel="external">实例地址</a></p>
<iframe src="https://codepen.io/yangbo5207/embed/LkyEwd" allowfullscreen="allowfullscreen" style="width: 100%; height: 300px" frameborder="0"></iframe>

<h2 id="replace"><a href="#replace" class="headerlink" title="replace"></a>replace</h2><p>布尔型，默认值为false，设置为true的时候，表示可以用模板内容替换自定义的元素标签。</p>
<p>在template的例子中，我们发现渲染结果中包含有自定义的元素<hello></hello>，很显然，这并不是我们想要的渲染结果，因此将replace设置为true之后，hello标签将会消失</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">app.directive(<span class="string">'hello'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> &#123;</div><div class="line">    <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">    <span class="attr">template</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, attrs</span>) </span>&#123;</div><div class="line">      <span class="keyword">return</span> <span class="string">'&lt;div&gt;'</span>+ attrs.title +<span class="string">'&lt;/div&gt;'</span></div><div class="line">    &#125;</div><div class="line">  &#125;;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>渲染结果如下，hello标签消失不见</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>message<span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="templateUrl"><a href="#templateUrl" class="headerlink" title="templateUrl"></a>templateUrl</h2><p>字符串或者函数，可选参数</p>
<p>可以使一个代表html文件路径的字符串，也可以是一个函数，大致意思与template一样。</p>
<p>在本地开发时，需要运行一个服务器，不然使用templateUrl会报错</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Cross origin request script(cors)</div></pre></td></tr></table></figure>
<p>由于加载html模板是通过异步加载，若加载大量的模板会拖慢网站的速度，这里有一个技巧，就是先缓存模板，你可以先在你的index页面加载好，将下列代码作为你页面的一部分包含在内</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/ng-template"</span> <span class="attr">id</span>=<span class="string">"demo.html"</span>&gt;</span><span class="javascript"></span></div><div class="line">  &lt;div&gt;<span class="xml"><span class="comment">&lt;!--这里是模板内容--&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></div><div class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div></pre></td></tr></table></figure>
<p>这里的id属性就是被设置在templateUrl上用的</p>
<p>另外一种方法缓存如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">app.run(<span class="function"><span class="keyword">function</span>(<span class="params">$templateCache</span>) </span>&#123;</div><div class="line">  $templateCache.put(<span class="string">'template.html'</span>, <span class="string">'&lt;div&gt;template&lt;/div&gt;'</span>);</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h2 id="scope"><a href="#scope" class="headerlink" title="scope"></a>scope</h2><p>布尔值或者对象，可选参数，默认值为false，表示继承父级作用域。</p>
<p>如果值为true，表示继承父作用域，并创建自己的作用域（子作用域）</p>
<p>如果为对象， <code>{}</code> ，则表示创建一个全新的隔离作用域。</p>
<p>首先我们需要来了解一下 <code>scope</code> 的继承机制。我们使用ng-controller这个指令来举例。我们都知道ng-controller可以从父作用域中继承并创建一个新的子作用域。如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;div ng-app=&quot;app&quot; ng-init=&quot;aaa=&apos;parent&apos;&quot;&gt;</div><div class="line">    parentNode: &#123;&#123;aaa&#125;&#125;</div><div class="line">    </div><div class="line">    &lt;div ng-controller=&quot;demoController&quot;&gt;</div><div class="line">        childrenNode: &#123;&#123;aaa&#125;&#125;</div><div class="line">    &lt;/div&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'demoController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.aaa = <span class="string">"children"</span>;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/ZOKbzR" target="_blank" rel="external">实例地址</a></p>
<iframe style="width: 100%; height: 300px" src="https://codepen.io/yangbo5207/embed/ZOKbzR" allowfullscreen="allowfullscreen" frameborder="0" class="loading"></iframe>

<p>这时页面的显示结果为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">parentNode: parent</div><div class="line">childrenNode: children</div></pre></td></tr></table></figure>
<p>当时当我们并没有在demoController的作用域中给aaa赋值，也就是在上例中删除这一句$scope.aaa = “children”;，那么执行结果就为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">parentNode: parent</div><div class="line">childrenNode: parent</div></pre></td></tr></table></figure>
<p>注意：如果一个元素上有多个指令都使用了隔离作用域，那么只有其中一个可以生效，只有指令模板中的根元素才能获得一个新的作用域，这时候，scope就被设置为true了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;div ng-app=&quot;app&quot; ng-init=&quot;aaa=&apos;parent&apos;&quot;&gt;</div><div class="line">    parentNode: &#123;&#123;aaa&#125;&#125;</div><div class="line">    </div><div class="line">    &lt;div ng-controller=&quot;demoController01&quot;&gt;</div><div class="line">        childrenNode: &#123;&#123;aaa&#125;&#125;</div><div class="line">        </div><div class="line">        &lt;div ng-controller=&quot;demoController02&quot;&gt;</div><div class="line">            lastNode: &#123;&#123;aaa&#125;&#125;</div><div class="line">        &lt;/div&gt;</div><div class="line">    &lt;/div&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'demoController01'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.aaa = <span class="string">"children"</span>;</div><div class="line">&#125;)</div><div class="line">.controller(<span class="string">'demoController02'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.aaa = <span class="string">"last"</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/LkypEK" target="_blank" rel="external">实例地址</a></p>
<iframe style="width: 100%; height: 300px" src="https://codepen.io/yangbo5207/embed/LkypEK" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

<p>接下来，我们通过一个简单明了的例子来说明scope取值的不同差别</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span>&gt;</span></div><div class="line">    parent: &#123;&#123; name &#125;&#125; </div><div class="line">    <span class="tag">&lt;<span class="name">br</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">ng-model</span>=<span class="string">"name"</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">my-directive</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'mainController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.name = <span class="string">"Jake"</span>;</div><div class="line">&#125;)</div><div class="line">.directive(<span class="string">'myDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">scope</span>: <span class="literal">false</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span>: <span class="string">''</span> +</div><div class="line">            <span class="string">'&lt;div&gt;'</span> +</div><div class="line">                <span class="string">'childNode: &#123;&#123; name &#125;&#125; '</span> +</div><div class="line">                <span class="string">'&lt;br /&gt;'</span> +</div><div class="line">                <span class="string">'&lt;input type="text" ng-model="name"&gt;'</span> +</div><div class="line">            <span class="string">'&lt;/div&gt;'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/yJbYgE?editors=1010" target="_blank" rel="external">实例地址</a></p>
<iframe style="width: 100%; height: 300px" src="https://codepen.io/yangbo5207/embed/yJbYgE" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

<p>点击上面的实例地址，我们可以依次改变scope的值为false, true, {}，结果发现</p>
<ul>
<li>false：儿子继承父亲的值，改变父亲的值，儿子的值也随着改变，反之亦然，这就是继承且不隔离</li>
<li>true：儿子继承父亲的值，改变父亲的值，儿子的值也随着改变，但是改变儿子的值，父亲的值并没有改变，这就是继承但是隔离</li>
<li>{}：没有继承父亲的值，所以儿子的值为空，改变任何一方的值都不会影响另一方，这就是不继承且隔离</li>
</ul>
<blockquote>
<p>当想要创建一个可重用的组件时，隔离作用域是一个很好的选择，通过隔离作用域，我们可以确保指令是独立的，并且可以轻松的插入到任何HTML APP中，并且这种做法防止了父作用域被污染。</p>
</blockquote>
<p><strong>隔离作用域可以通过绑定策略来访问父作用域的属性</strong></p>
<p>我们来看一个例子</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">ng-model</span>=<span class="string">"color"</span> <span class="attr">placeholder</span>=<span class="string">"Enter a color"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">hello-world</span>&gt;</span><span class="tag">&lt;/<span class="name">hello-world</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'mainController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">&#125;)</div><div class="line">.directive(<span class="string">'helloWorld'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">scope</span>: <span class="literal">false</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span>: <span class="string">'&lt;p style="background-color:&#123;&#123;color&#125;&#125;"&gt;Hello world!&lt;/p&gt;'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>运行上面的代码，我们在input中输入颜色值，比如red，那么hello world一行的背景就会变成红色。动手试试！</p>
<p><a href="https://codepen.io/yangbo5207/pen/AXRWVw" target="_blank" rel="external">实例地址</a></p>
<iframe style="width: 100%; height: 300px" src="https://codepen.io/yangbo5207/embed/AXRWVw" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

<p>但是，当我们将scope的值设置为{}时，再次运行代码就发现页面并不能成功的完整显示了.这是因为{}让helloWorld指令产生了隔离作用域，没办法从父级作用域中继承到color的值了。</p>
<p>所以在template中的变成了依赖于自己的作用域，而不是依赖于父级作用域。因此我们需要一些办法来让隔离作用域能够读取父级作用域的属性，这个方法就是绑定策略。</p>
<p>下面我们来探索设置这种绑定策略的几种方法</p>
<p><strong> 方法一 使用@来进行单向文本（字符串）绑定 </strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">ng-model</span>=<span class="string">"color"</span> <span class="attr">placeholder</span>=<span class="string">"Enter a color"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">hello-world</span> <span class="attr">color-attr</span>=<span class="string">"&#123;&#123;color&#125;&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">hello-world</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'mainController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">&#125;)</div><div class="line">.directive(<span class="string">'helloWorld'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">scope</span>: &#123;</div><div class="line">            <span class="attr">color</span>: <span class="string">'@colorAttr'</span></div><div class="line">        &#125;,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span>: <span class="string">'&lt;p style="background-color:&#123;&#123;color&#125;&#125;"&gt;Hello world!&lt;/p&gt;'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/AXRWVw" target="_blank" rel="external">实例地址</a></p>
<iframe style="width: 100%; height: 300px" src="https://codepen.io/yangbo5207/embed/AXRWVw" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

<p>这种办法只能单向，通过在运行的指令DOM上设置 <code>color-attr</code> 属性，并且采用双花括号绑定某个模型值。当然，我们也可以直接在这里绑定字符串的颜色值，如 <code>color-attr=&quot;red&quot;</code>.</p>
<p>因此当表达式的值发生变化时，属性color-attr的值也会发生变化，通过单向绑定该值，就可以改变隔离作用域中的属性color.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">如果绑定的隔离作用域属性名与元素的属性名相同，则可以采用缺省写法</div><div class="line"> </div><div class="line"> // html</div><div class="line"> &lt;hello-world color=&quot;&#123;&#123;color&#125;&#125;&quot;&gt;&lt;/hello-world&gt;</div><div class="line"> </div><div class="line"> // js</div><div class="line"> app.directive(&apos;helloWorld&apos;, function() &#123;</div><div class="line">    return &#123;</div><div class="line">        scope: &#123; color: &apos;@&apos; &#125;,</div><div class="line">        ...</div><div class="line">    &#125;</div><div class="line"> &#125;)</div></pre></td></tr></table></figure>
<p><strong> 方法二、使用’=’进行双向绑定 </strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">ng-model</span>=<span class="string">"color"</span> <span class="attr">placeholder</span>=<span class="string">"Enter a color"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">br</span> /&gt;</span></div><div class="line">    &#123;&#123; color &#125;&#125;</div><div class="line">    <span class="comment">&lt;!-- 这里要注意写法与@绑定的不同 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">hello-world</span> <span class="attr">color</span>=<span class="string">"color"</span>&gt;</span><span class="tag">&lt;/<span class="name">hello-world</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'mainController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.color = <span class="string">'red'</span>;</div><div class="line">&#125;)</div><div class="line">.directive(<span class="string">'helloWorld'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">scope</span>: &#123;</div><div class="line">            <span class="attr">color</span>: <span class="string">'='</span></div><div class="line">        &#125;,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span>: <span class="string">'&lt;div&gt;&lt;p style="background-color:&#123;&#123;color&#125;&#125;"&gt;'</span> +</div><div class="line">            <span class="string">'Hello world!'</span> +</div><div class="line">        <span class="string">'&lt;/p&gt;'</span> + </div><div class="line">        <span class="string">'&lt;input type="text" ng-model="color"&gt;&lt;/div&gt;'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/WxjQPK?editors=1010" target="_blank" rel="external">实例地址</a></p>
<p>此处也采用了类似的缺省写法。</p>
<p>这里需要注意的是，我们要直接在指令元素设置属性时，是直接将实际的作用域模型复制给该属性</p>
<p>这样一个双向绑定被建立了，改变任何一个input值都会改变另外一个值。</p>
<p><strong> 方法三、使用’&amp;’调用父作用域中的函数 </strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">ng-model</span>=<span class="string">"name"</span> <span class="attr">placeholder</span>=<span class="string">"Enter name"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">br</span> /&gt;</span></div><div class="line">    &#123;&#123; name &#125;&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">hello-world</span> <span class="attr">say</span>=<span class="string">"say()"</span> <span class="attr">name</span>=<span class="string">"&#123;&#123;name&#125;&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">hello-world</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'mainController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.name = <span class="string">"yangbo"</span>;</div><div class="line">    $scope.say = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">        alert(<span class="string">'hello!'</span>);</div><div class="line">    &#125;</div><div class="line">&#125;)</div><div class="line">.directive(<span class="string">'helloWorld'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">scope</span>: &#123;</div><div class="line">            <span class="attr">name</span>: <span class="string">'@'</span>,</div><div class="line">            <span class="attr">say</span>: <span class="string">'&amp;'</span></div><div class="line">        &#125;,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span>: <span class="string">'&lt;button type="button" ng-bind="name" ng-init="say()"&gt;&lt;/button&gt;'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/grWaJX?editors=1010" target="_blank" rel="external">实例地址</a></p>
<p>同样采用了缺省写法，运行之后，弹出窗口！</p>
<h2 id="transclude"><a href="#transclude" class="headerlink" title="transclude"></a>transclude</h2><p>布尔值或者字符 <code>element</code> ，默认值为 <code>false</code></p>
<p>这个配置选项可以让我们提取包含在指令那个元素里面的内容，再将它放置在指令模板的特定位置。当我们开启 <code>transclude</code> 之后，我们就可以使用 <code>ng-transclude</code> 来指明应该在什么地方放置 <code>transclude</code> 的内容</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"a"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>china<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">hello-world</span>&gt;</span></div><div class="line">            &#123;&#123;name&#125;&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">hello-world</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'mainController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.name = <span class="string">"yangbo5207"</span>;</div><div class="line">&#125;)</div><div class="line">.directive(<span class="string">'helloWorld'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">scope</span>: &#123;&#125;,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span>: <span class="string">'&lt;div class="b"&gt;&lt;div ng-transclude&gt;你看不见我&lt;/div&gt;&lt;/div&gt;'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>运行上面的代码，输出</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">china</div><div class="line">yangbo5207</div></pre></td></tr></table></figure>
<p>我们查看渲染出来的html，结果为</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"mainController"</span> <span class="attr">class</span>=<span class="string">"ng-scope"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"a"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>china<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"b ng-isolate-scope"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-transclude</span>=<span class="string">""</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"ng-binding ng-scope"</span>&gt;</span>yangbo5207<span class="tag">&lt;/<span class="name">span</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<p>另外当开启 <code>transclude</code> 时，会创建一个新的 <code>transclude</code> 空间，并且继承父作用域（也就是 <code>scope</code> 设置的隔离作用域）</p>
<p>从上面例子我们知道，当 <code>transclude</code> 的值被设置为 <code>true</code> 时，嵌入的内容为，但是如果将它的值设置为 <code>element</code> 呢，我们可以在上例的基础上进行一个简单的修改，发现，嵌入内容为整个元素</p>
<p><strong> 注意：在一个指令的模板中，只能声明一次ng-transclude </strong></p>
<p>那么问题来了，如果我们想要把嵌入内容多次放入我们的模板中怎么办？</p>
<p>可以使用 <code>$transclude</code> ，后面会讲到！也可以使用 <code>complie</code> 函数中，里面的 <code>transcludeFn</code> 参数，后面会讲到！或者使用link连接函数</p>
<h2 id="controller"><a href="#controller" class="headerlink" title="controller"></a>controller</h2><p>可以是一个字符串或者函数。</p>
<p>若为字符串，则将字符串当做控制器的名字，来查找注册在应用中的控制器的构造函数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.directive(<span class="string">'myDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">        </div><div class="line">        <span class="comment">// 会查找模块中其他被名为targetController的控制器</span></div><div class="line">        controller: <span class="string">'targetController'</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div><div class="line">.controller(<span class="string">'targetController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope, $element, $attrs, $transclude</span>) </span>&#123;</div><div class="line">    <span class="comment">// 控制器逻辑放在这里</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>当然，也可以直接在指令内部定义为匿名函数，同样可以注入任何服务</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.directive(<span class="string">'myDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span>: <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">controller</span>: <span class="function"><span class="keyword">function</span>(<span class="params">$scope, $element, $attrs, $transclude</span>) </span>&#123;</div><div class="line">            <span class="comment">// 控制器逻辑</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>另外还有一些特殊的服务可以注入，</p>
<ul>
<li>$scope 与指令元素相关联的作用域</li>
<li>$element 当前指令对应的元素</li>
<li>$attrs 当前元素的属性组成的对象</li>
<li>$transclude 嵌入链接函数，实际被执行用来克隆元素和操作DOM中的函数（除非是用来定义一些可复用的行为，否则一般不推荐在这使用）</li>
</ul>
<p>指令的控制器和link函数（后面会讲到）可以进行互换。区别在于，控制器主要用来提供可在指令间复用的行为，可对外提供与外部交互的接口，但是link链接只能在当前指令内部中定义行为，且无法在指令间复用。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">test-directive</span>=<span class="string">"dateType"</span> <span class="attr">url</span>=<span class="string">"http://www.tigerbrokers.com"</span>&gt;</span>外面的世界很精彩。<span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.directive(<span class="string">'testDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">controller</span>: <span class="function"><span class="keyword">function</span>(<span class="params">$scope, $element, $attrs, $transclude, $log</span>) </span>&#123;</div><div class="line">            $transclude(<span class="function"><span class="keyword">function</span>(<span class="params">clone</span>) </span>&#123;</div><div class="line">                <span class="keyword">var</span> a = angular.element(<span class="string">'&lt;a&gt;'</span>);</div><div class="line">                a.attr(<span class="string">'href'</span>, $attrs.url);</div><div class="line">                a.text(clone.text());</div><div class="line">                $element.append(a);</div><div class="line">            &#125;);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/JKNGpN?editors=1011" target="_blank" rel="external">实例地址</a></p>
<p><code>$transclude</code> 可以接受两个参数，第一个是$scope，第二个是才有参数clone的回调函数。</p>
<p>而这个clone实际上就是嵌入的内容。可以在根据它做很多DOM操作。</p>
<p>它还有最简单的用法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.directive(<span class="string">'testDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">controller</span>: <span class="function"><span class="keyword">function</span>(<span class="params">$scope, $element, $attrs, $transclude, $log</span>) </span>&#123;</div><div class="line">            </div><div class="line">            <span class="comment">// 这里的$transclude就是嵌入的内容</span></div><div class="line">            <span class="keyword">var</span> a = $transclude();</div><div class="line">            $element.append(a);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>但是我们要注意，使用 <code>$transclude</code> 会生成一个新的作用域。默认情况下，如果我们简单使用 <code>$transclude()</code> ，那么其作用域就是 <code>$transclude</code> 生成的作用域。但是如果我们使用 <code>$transclude($scope, function(clone) {})</code> ，那么作用域就是 <code>directive</code> 的作用域了。</p>
<p>当然问题又来了，如果我们想使用父级作用域呢？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$scope.$parent</div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"parentController"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-controller</span>=<span class="string">"sonController"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">test-directive</span> <span class="attr">url</span>=<span class="string">"http://www.tigerbrokers.com"</span>&gt;</span>炒美股，上老虎2<span class="tag">&lt;/<span class="name">test-directive</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'parentController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.title = <span class="string">"PARENT TIGER"</span>;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">.controller(<span class="string">'sonController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.title = <span class="string">"CHILDREN TIGER"</span>;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">.directive(<span class="string">'testDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">        <span class="attr">controller</span>: <span class="function"><span class="keyword">function</span>(<span class="params">$scope, $element, $attrs, $transclude, $log</span>) </span>&#123;</div><div class="line">            <span class="keyword">var</span> a = $transclude();</div><div class="line">            $element.append(a);</div><div class="line">            $log.info($scope.title);</div><div class="line">            $log.info($scope.$parent.title);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/XKRdJg?editors=1011" target="_blank" rel="external">实例地址</a></p>
<h2 id="controllerAs"><a href="#controllerAs" class="headerlink" title="controllerAs"></a>controllerAs</h2><p>angualr 1.2之后，就已经开始支持controllerAs的语法，这样我们就可以不用将属性和方法挂载到$scope上，而是this上。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"app"</span> <span class="attr">ng-controller</span>=<span class="string">"demoController as demo"</span>&gt;</span>&#123;&#123; demo.name &#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">angular.module(<span class="string">'app'</span>, [])</div><div class="line">.controller(<span class="string">'demoController'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">this</span>.name = <span class="string">"Jake"</span>;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/vKmGyb?editors=1011" target="_blank" rel="external">实例地址</a></p>
<p>我们可以从实例中看出，这里的 <code>this</code> 就是指的 <code>as</code> 后面的那个别名。然后我们在表达式中就可以使用这个别名</p>
<p>我们知道，在 <code>directive</code> 中的 <code>controller</code> ，主要职能是对外提供交互接口，而结合 <code>require</code> 与 <code>link</code> ，因此我们常常会利用这样的语法而非上面例子中的 <code>$scope</code>. </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">app.directive(<span class="string">'testDirective'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">controller</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">            <span class="keyword">this</span>.name = <span class="string">"Jake"</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h2 id="require"><a href="#require" class="headerlink" title="require"></a>require</h2><p>字符串或者数组，字符串代表另一个指令的名字，它将作为 <code>link</code> 函数的第四个参数。具体用法我们可以举例子来说明。</p>
<p>假设现在我们要编写两个指令，两个指令的 <code>link</code> 函数中存在许多重合的方法，这时候我们就可以将这些重复的方法写在第三个指令的 <code>controller</code> 中，然后在这两个指令中，使用 <code>require</code> 将第三个指令引入进来，然后我们就可以通过 <code>link</code> 连接函数的第四个参数引用这些重合的方法了</p>
<p>指令之间常常通过这种方式进行交互</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">ng-app</span>=<span class="string">"expanderModule"</span> <span class="attr">ng-controller</span>=<span class="string">"SomeController"</span> <span class="attr">class</span>=<span class="string">"wrap"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">accordion</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">expander</span> <span class="attr">class</span>=<span class="string">'expander'</span> <span class="attr">ng-repeat</span>=<span class="string">'expander in expanders'</span> <span class="attr">expander-title</span>=<span class="string">'expander.title'</span>&gt;</span></div><div class="line">            &#123;&#123;expander.text&#125;&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">expander</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">accordion</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> expModule=angular.module(<span class="string">'expanderModule'</span>,[])</div><div class="line">expModule.directive(<span class="string">'accordion'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span> : <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">replace</span> : <span class="literal">true</span>,</div><div class="line">        <span class="attr">transclude</span> : <span class="literal">true</span>,</div><div class="line">        <span class="attr">template</span> : <span class="string">'&lt;div ng-transclude&gt;&lt;/div&gt;'</span>,</div><div class="line">        <span class="attr">controller</span> : <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">            <span class="keyword">var</span> expanders = [];</div><div class="line">            <span class="keyword">this</span>.gotOpened = <span class="function"><span class="keyword">function</span>(<span class="params">selectedExpander</span>) </span>&#123;</div><div class="line">                angular.forEach(expanders, <span class="function"><span class="keyword">function</span>(<span class="params">expander</span>) </span>&#123;</div><div class="line">                    <span class="keyword">if</span> (selectedExpander != expander) &#123;</div><div class="line">                        expander.showMe = <span class="literal">false</span>;</div><div class="line">                    &#125;</div><div class="line">                &#125;);</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">this</span>.addExpander = <span class="function"><span class="keyword">function</span>(<span class="params">expander</span>) </span>&#123;</div><div class="line">                expanders.push(expander);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">expModule.directive(<span class="string">'expander'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">restrict</span> : <span class="string">'EA'</span>,</div><div class="line">        <span class="attr">replace</span> : <span class="literal">true</span>,</div><div class="line">        <span class="attr">transclude</span> : <span class="literal">true</span>,</div><div class="line">        <span class="attr">require</span> : <span class="string">'^?accordion'</span>,</div><div class="line">        <span class="attr">scope</span> : &#123;</div><div class="line">            <span class="attr">title</span> : <span class="string">'=expanderTitle'</span></div><div class="line">        &#125;,</div><div class="line">        <span class="attr">template</span> : <span class="string">'&lt;div&gt;'</span></div><div class="line">                   + <span class="string">'&lt;div class="title" ng-click="toggle()"&gt;&#123;&#123;title&#125;&#125;&lt;/div&gt;'</span></div><div class="line">                   + <span class="string">'&lt;div class="body" ng-show="showMe" ng-transclude&gt;&lt;/div&gt;'</span></div><div class="line">                   + <span class="string">'&lt;/div&gt;'</span>,</div><div class="line">        <span class="attr">link</span> : <span class="function"><span class="keyword">function</span>(<span class="params">scope, element, attrs, accordionController</span>) </span>&#123;</div><div class="line">            scope.showMe = <span class="literal">false</span>;</div><div class="line">            accordionController.addExpander(scope);</div><div class="line">            scope.toggle = <span class="function"><span class="keyword">function</span> <span class="title">toggle</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                scope.showMe = !scope.showMe;</div><div class="line">                accordionController.gotOpened(scope);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">expModule.controller(<span class="string">"SomeController"</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>&#123;</div><div class="line">    $scope.expanders = [&#123;</div><div class="line">        <span class="attr">title</span> : <span class="string">'Click me to expand'</span>,</div><div class="line">        <span class="attr">text</span> : <span class="string">'Hi there folks, I am the content that was hidden but is now shown.'</span></div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">title</span> : <span class="string">'Click this'</span>,</div><div class="line">        <span class="attr">text</span> : <span class="string">'I am even better text than you have seen previously'</span></div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">title</span> : <span class="string">'Test'</span>,</div><div class="line">        <span class="attr">text</span> : <span class="string">'test'</span></div><div class="line">    &#125;];</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p><a href="https://codepen.io/yangbo5207/pen/kXyvad?editors=1011" target="_blank" rel="external">实例地址</a></p>
<p>实例说明， <code>controller</code> 是用来与不同指令之间通信的。</p>
<p>另外我们可以在 <code>require</code> 的参数值加上下面的某个前缀，这回改变查找控制器的行为。</p>
<ul>
<li>没有前缀 指令会在自身提供的控制器中进行查找，如果找不到任何控制器，则会抛出一个error</li>
<li>? 若在当前指令中没有找到所需的控制器，则会将null传给link链接函数的第四个参数</li>
<li>^ 如果在当前的指令中没有找到所需的控制器，则会查找父元素的控制器</li>
<li>?^ 如果在当前和父元素中都没有找到控制器，则会将null传给link</li>
</ul>
<h2 id="angular指令编译过程"><a href="#angular指令编译过程" class="headerlink" title="angular指令编译过程"></a>angular指令编译过程</h2><p>首先加载 <code>angular</code> 库，查找 <code>ng-app</code> 指令，从而找到应用的边界，根据 <code>ng-app</code> 划定的作用域来调用 <code>$compile</code> 服务进行编译。</p>
<p><code>angular</code> 会遍历整个 <code>html</code> 文档，并根据 <code>js</code> 中指令的定义来处理在页面上声明的各个指令，按照指令的优先级排列，根据指令中的配置参数( <code>template</code> , <code>replace</code>, <code>transclude</code>等)转换DOM，然后就开始按照顺序执行各指令的 <code>compile</code> 函数（如果指令上有定义 <code>compile</code> 函数）对模板自身进行转换。</p>
<blockquote>
<p>此处的compile函数值什么指令中配置的，与上面说的$compile服务不一样</p>
</blockquote>
<p>每个compile函数执行完后会返回一个link函数，所有的link函数会合成一个大的link函数，然后这个大的link函数就会被执行， 主要做数据绑定，通过 DOM上注册监听器来动态修改scope中的数据，或者是使用$watchs监听scope中变量来修改DOM，从而建立双向绑定等等。</p>
<p>若我们的指令中没有配置compile函数，那我们配置的link函数就会运行，她做的事情大致跟上面compile返回之后所有的link合成的大link函数差不多。</p>
<p><strong> 所以，在指令中compile与link选项是互斥的，如果同时设置了这两项，那么就会把compile所返回的函数当做是链接函数，而link选项本身会被忽略。 </strong></p>
<h2 id="compile编译函数与link链接函数"><a href="#compile编译函数与link链接函数" class="headerlink" title="compile编译函数与link链接函数"></a>compile编译函数与link链接函数</h2><p><code>cmopile</code>选项可以返回一个对象或者函数，在这里我们可以在指令和实时数据被放到DOM之前进行DOM操作，比如我们可以在这里进行添加或者删除节点的DOM操作。</p>
<p>所以编译函数是负责对模板的DOM进行转换，并且仅仅只会运行一次</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//compile函数的语法</span></div><div class="line">compile:<span class="function"><span class="keyword">function</span> <span class="title">compile</span>(<span class="params">tElement,tAttrs,transclude</span>)</span>&#123;</div><div class="line">      <span class="keyword">return</span>&#123;</div><div class="line">        <span class="attr">pre</span>:<span class="function"><span class="keyword">function</span> <span class="title">preLink</span>(<span class="params">scope,iElement,iAttrs,controller</span>)</span>&#123;&#125;,</div><div class="line">        <span class="attr">post</span>:<span class="function"><span class="keyword">function</span> <span class="title">postLink</span>(<span class="params">scope,iElement,iAttrs,controller</span>)</span>&#123;&#125;</div><div class="line">      &#125;</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>对于我们编写的大部分指令来说，并不需要对模板进行转换，所以大部分情况只需要编写link函数就行。</p>
<blockquote>
<p>preLink函数会在编译阶段之后，指令链接到子元素之前执行，类似的，postLink会在指令链接到子元素之后执行。这意味着，为了不破坏绑定过程，如果你需要修改DOM结构，你应该在postLink函数中来做这件事情。</p>
</blockquote>
<p>link函数负责将作用域与DOM进行链接</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//link链接函数</span></div><div class="line">link:<span class="function"><span class="keyword">function</span> <span class="title">postLink</span>(<span class="params">scope,iElement,iAttrs</span>)</span>&#123;&#125;</div></pre></td></tr></table></figure>
<p>若指令中定义有require选项，则link函数会有第四个参数，代表控制器或者所依赖的指令的控制器（上面require选项例子已有例子）</p>
<blockquote>
<p>转载来自 <a href="https://segmentfault.com/a/1190000005851663#articleHeader12" target="_blank" rel="external">https://segmentfault.com/a/1190000005851663#articleHeader12</a></p>
</blockquote>

      
    </div>

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Angular/" <i class="fa fa-tag"></i> Angular</a>
          
            <a href="/tags/directive/" <i class="fa fa-tag"></i> directive</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/06/27/other-20170627前端跨域整理/" rel="next" title="前端跨域整理">
                <i class="fa fa-chevron-left"></i> 前端跨域整理
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/06/30/nodejs-20170630NodeJs-实现简单服务器/" rel="prev" title="NodeJs 实现简单服务器">
                NodeJs 实现简单服务器 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpeg"
               alt="石艷軍" />
          <p class="site-author-name" itemprop="name">石艷軍</p>
           
              <p class="site-description motion-element" itemprop="description">你总是这样轻言放弃的话，无论过多久都只会原地踏步。</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">8</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/SYJUN" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.jianshu.com/u/a28c201fe481" target="_blank" title="简书">
                  
                    <i class="fa fa-fw fa-heartbeat"></i>
                  
                  简书
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#restrict"><span class="nav-number">1.</span> <span class="nav-text">restrict</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#priority"><span class="nav-number">2.</span> <span class="nav-text">priority</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#terminal"><span class="nav-number">3.</span> <span class="nav-text">terminal</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#template"><span class="nav-number">4.</span> <span class="nav-text">template</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#replace"><span class="nav-number">5.</span> <span class="nav-text">replace</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#templateUrl"><span class="nav-number">6.</span> <span class="nav-text">templateUrl</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#scope"><span class="nav-number">7.</span> <span class="nav-text">scope</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#transclude"><span class="nav-number">8.</span> <span class="nav-text">transclude</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#controller"><span class="nav-number">9.</span> <span class="nav-text">controller</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#controllerAs"><span class="nav-number">10.</span> <span class="nav-text">controllerAs</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#require"><span class="nav-number">11.</span> <span class="nav-text">require</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#angular指令编译过程"><span class="nav-number">12.</span> <span class="nav-text">angular指令编译过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#compile编译函数与link链接函数"><span class="nav-number">13.</span> <span class="nav-text">compile编译函数与link链接函数</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">石艷軍</span>
</div>

<!--

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

-->

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i> 访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i> 总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>


        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>

